import { request } from "../../request/index.js";
Page({
    data:{
        leftMenuList:[],
        rightContent:[],
        currentMenu:0,
        scrollTop:0
    },
    //接口返回的数据
    Cates:[],
    onLoad(){
        const Cates=wx.getStorageSync("cates");//从本地缓存中读取数据
        if(!Cates){
            this.getMenuList();
        }else{
            if(Date.now()-Cates.time>1000*60*5){//5分钟之内会从缓存中获取数据，否则会发送请求获取数据
                this.getMenuList();
            }else{
                this.Cates=Cates.data;
                let leftMenuList=this.Cates.map((item)=>{
                    return item.cat_name;
                });
                let rightContent=this.Cates[0].children;
                this.setData({
                    leftMenuList,
                    rightContent
                });
            };
        };
    },
    //获取左侧和右侧数据事件监听
    async getMenuList(){
        // request({
        //     url:"/categories",
        //     method:"get"
        // }).then((result)=>{
        //     this.Cates=result.data.message;
        //     wx.setStorageSync("cates",{time:Date.now(),data:this.Cates});//存入本地缓存
        //     let leftMenuList=this.Cates.map((item)=>{
        //         return item.cat_name;
        //     });
        //     let rightContent=this.Cates[0].children;
        //     this.setData({
        //         leftMenuList,
        //         rightContent
        //     });
        // });
        let res=await request({url:"/categories",method:"get"});
        this.Cates=res.data.message;
        wx.setStorageSync("cates",{time:Date.now(),data:this.Cates});//存入本地缓存
        let leftMenuList=this.Cates.map((item)=>{
            return item.cat_name;
        });
        let rightContent=this.Cates[0].children;
        this.setData({
            leftMenuList,
            rightContent
        });
    },
    //点击左侧菜单事件监听
    selectMenu(e){
        let {index}=e.currentTarget.dataset;
        let rightContent=this.Cates[index].children;
        this.setData({
            currentMenu:index,
            rightContent,
            scrollTop:0
        });
    }
})